package net.example.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import net.example.manager.GGApplication;

/**
 * Created by ZN on 2015/12/3.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    public String currentUserID ;
    private static DatabaseHelper instance;
    public static  DatabaseHelper getInstance(){
            return instance;
    }

    public static void resetIntance(String loginUserID){
        if(instance == null)
        {
            instance = new DatabaseHelper(GGApplication.applicationContext ,loginUserID);
            return;
        }
        if(instance.currentUserID == loginUserID){
            return;
        }
        instance = new DatabaseHelper(GGApplication.applicationContext ,loginUserID);
    }

    private DatabaseHelper(Context context ,String loginUserID) {
        super(context, "ggtalk" + "_" + loginUserID + ".db", null, DATA_BASE_VERSION);
        this.currentUserID = loginUserID;
    }

    public static final String GGUser = "GGUser";
    public static final String GGGroup = "GGGroup";
    public static final String ChatMessageRecord = "ChatMessageRecord";
    public static final String LastChatRecord = "LastChatRecord";
    private static final int DATA_BASE_VERSION = 2;
    private static final String GGUserTable = "CREATE TABLE if not exists  [GGUser](" +
            "[UserID] [varchar] (50) primary key NOT NULL ," +
            "[PasswordMD5] [varchar] (100) NOT NULL ," +
            "[Name] [nvarchar] (50) NOT NULL ," +
            "[Friends] [nvarchar] (4000) NOT NULL ," +
            "[CommentNames] [nvarchar] (4000) NOT NULL ," +
            "[OrgID] [nvarchar] (20) NOT NULL ," +
            "[Signature] [nvarchar] (100) NOT NULL ," +
            "[HeadImageIndex] [integer] NOT NULL ," +
            "[HeadImageData] [image] NULL ," +
            "[Groups] [varchar] (1000) NOT NULL ," +
            "[CreateTime] [BIGINT] NOT NULL ," +
            "[Version] [integer] NOT NULL)";

    private static final String GGGroupTable = "CREATE TABLE if not exists  [GGGroup](" +
            "[GroupID] [varchar] (20) primary key NOT NULL ," +
            "[Name] [nvarchar] (20) NOT NULL ," +
            "[CreatorID] [varchar] (20) NOT NULL ," +
            "[Announce] [nvarchar] (200) NOT NULL ," +
            "[Members] [nvarchar] (4000) NOT NULL ," +
            "[IsPrivate] [integer] NOT NULL ," +
            "[CreateTime] [BIGINT] NOT NULL ," +
            "[Version] [integer] NOT NULL)";

    private static final String ChatMessageRecordTable = "CREATE TABLE if not exists  [ChatMessageRecord](" +
            "[AutoID] [integer] primary key autoincrement," +
            "[SpeakerID] [varchar] (20) NOT NULL ," +
            "[AudienceID] [varchar] (20) NOT NULL ," +
            "[IsGroupChat] [integer] NOT NULL ," +
            "[IsRead] [integer] NOT NULL ," +
            "[Content] [BLOB] NOT NULL ," +
            "[OccureTime] [BIGINT] NOT NULL," +
            "[Tag] [varchar] (50)  NOT NULL ); "+
            "CREATE INDEX idx_ChatMessageRecord ON ChatMessageRecord (" +
            "SpeakerID," +
            "AudienceID," +
            "OccureTime DESC);" +
            "CREATE INDEX idx2_ChatMessageRecord ON ChatMessageRecord (" +
            "AudienceID," +
            "IsGroupChat," +
            "OccureTime);";

    private static final String GGConfigTable = "CREATE TABLE if not exists  GGConfiguration(" +
            "[GGKey] [varchar](20) primary key NOT NULL," +
            "[GGValue] [nvarchar](1000) NOT NULL)";

    private static final String LastChatRecordTable = "CREATE TABLE if not exists  [LastChatRecord](" +
            "[SenderID] [varchar](20) primary key NOT NULL," +
//            "[IconRes] [integer] NOT NULL," +
            "[Title] [nvarchar] (50) NOT NULL," +
            "[LastWord] [nvarchar] (100) NOT NULL," +
            "[IsGroup] [integer] NOT NULL ," +
            "[UnReadCount] [integer] NOT NULL," +
            "[Time] [BIGINT] NOT NULL); " +
            "CREATE INDEX idx_LastChatRecord ON LastChatRecord (Time DESC);";



    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("drop table if exists GGUser");
        db.execSQL("drop table if exists GGGroup");
        db.execSQL("drop table if exists ChatMessageRecord");
        db.execSQL("drop table if exists GGConfiguration");
        db.execSQL("drop table if exists LastChatRecord");

        db.execSQL(GGUserTable);
        db.execSQL(GGGroupTable);
        db.execSQL(ChatMessageRecordTable);
        db.execSQL(GGConfigTable);
        db.execSQL(LastChatRecordTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

    }
}